using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.OleDb;

namespace LogAnalyzerIIS.Exporters
{
	partial class ExportToSQLWizard
	{
		private List<object[]> ODBCDataSources;		// List of ODBC data sources
		private bool oDBCProviderHasChanged = true;	// Fields on data provider page is changed

		/// <summary>
		/// Prepare the ODBC data provider page
		/// </summary>
		/// <returns>success</returns>
		private bool PrepareOdbc()
		{
			// Check if there is no any data sources in list of the ODBC data sources	
			if (cbODBCDataSources.Items.Count == 0)
			{
				// If not, then try to get ODBC enumerator type
				Type enumeratorType = Type.GetTypeFromProgID("MSDASQL Enumerator");

				if (enumeratorType == null)
				{
					// Show error message if fail getting the type
					MessageBox.Show("Cannot get ODBC enumerator list.", "ODBC data provider", MessageBoxButtons.OK, MessageBoxIcon.Error);

					// return failure
					return false;
				}

				// Get enumerator  
				DbDataReader dr = OleDbEnumerator.GetEnumerator(enumeratorType);

				if (dr == null)
				{
					// Show error message if enumerator can't be got
					MessageBox.Show("Cannot get ODBC provider list.", "ODBC data provider", MessageBoxButtons.OK, MessageBoxIcon.Error);
					return false;
				}

				// Clear ODBC data source combobox
				cbODBCDataSources.Items.Clear();

				// Create new list of ODBC data sources
				ODBCDataSources = new List<object[]>();

				// Move to the next data source
				while (dr.Read())
				{
					// Create array of objects for storing information about data source
					object[] objectArray = new object[dr.FieldCount];

					try
					{
						// Try to get information data source
						dr.GetValues(objectArray);
					}
					catch (Exception ex)
					{
						// show error message and return failure
						MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
						return false;
					}

					// Add information about ODBC data source to the list of ODBC data sources
					ODBCDataSources.Add(objectArray);

					// Add the data source name to the combobox
					cbODBCDataSources.Items.Add(objectArray[0].ToString());
				}
			}
			// return success
			return true;
		}

		/// <summary>
		/// Creates a connection string to connect to ODBC data provider from the data wizards fields
		/// </summary>
		/// <returns>Connection string</returns>
		private string GetOdbcConnectionString()
		{
			// Data source name
			string dataSourceName = "Dsn=" + ODBCDataSources[cbODBCDataSources.SelectedIndex][0].ToString();

			// Database name
			dataSourceName += ";dbq=" + tbODBCDataBaseName.Text;

			// Authentication
			if (tbODBCUserId.Text != "")
				dataSourceName += ";uid=" + tbSQLClientUserName.Text;

			if (tbODBCPassword.Text != "")
				dataSourceName += ";pwd=" + tbSQLClientPassword.Text;

			return dataSourceName;
		}

		/// <summary>
		/// Validate fields on "ODBC data provider" page and enable Next button
		/// </summary>
		private void TryOdbcNextButtonEnable()
		{
			// If data source and database are set then ...
			if ((cbODBCDataSources.Text != "") &&
				(tbODBCDataBaseName.Text != ""))
				btnNext.Enabled = true;
			else
				btnNext.Enabled = false;
		}

		/// <summary>
		/// Event which occurs then selection data source combobox on ODBC page is changed
		/// </summary>
		private void OdbcDataSourcesChanged(object sender, EventArgs e)
		{
			TryOdbcNextButtonEnable();
			oDBCProviderHasChanged = true;
		}

		/// <summary>
		/// Event which occurs then auth fields on ODBC page is changed
		/// </summary>
		private void OdbcAuthParameterChanged(object sender, EventArgs e)
		{
			oDBCProviderHasChanged = true;
		}

		/// <summary>
		/// Selecting an ODBC database file with OpenDialog
		/// </summary>
		private void btnOdbcBrowse_Click(object sender, EventArgs e)
		{
			OpenDataBaseFileDialog1.Title = "Select database file...";
			OpenDataBaseFileDialog1.Filter = "All files|*.*";
			OpenDataBaseFileDialog1.DefaultExt = "";

			if (OpenDataBaseFileDialog1.ShowDialog() == DialogResult.OK)
			{
				tbODBCDataBaseName.Text = OpenDataBaseFileDialog1.FileName;
				TryOdbcNextButtonEnable();
			}
		}
	}
}